<?php
/*********这里是测试数据*********/
//通道固定参数不用理会 也不要修改
$payment=[
	"payment_day_num"=>12,//每天最大刷还总和
];
//计划间隔时间
$interval_time="1800";
$interval_time_end="2160";
//初始化一个空数据，一会存放整体计划数据
$list=[];
//初始化一个空数组，一会存放单个计划数据
$plan=[];
//第一笔还款时间
$time = $time = date("Y-m-d H:i:s", time() + rand(100, 600));
//这里还款金额列表
$lists=["2037.5","1852.6","1700.4","1645.6","1874.7","1909.0","1980.2"];


//还款时间范围；意思从26号开始刷还到28号截止这个还款计划
$list_time = ["2019-04-26","2019-04-27","2019-04-28"];
$i=0;
foreach ($lists as $key => $money) {
	$i++;
	$repayment_time = repayment_time($time, $i, $key, $payment, $interval_time, $interval_time_end, '1', $list_time);
	$time = $repayment_time['time'];
	$i = $repayment_time['i'];
	//使用三刷一还模式
	//该参数是对应的$lists子账单，比如2037.5是还款总金额；模式是刷三笔还一笔；所以需要吧2037.5打散成3个金额刷出来。综合就是还款一次对应单个刷款金额，$moneys就是对应还款金额的打散数据
	$moneys = randnum_new($money, 3, "300.00", "2600.00", 1, 600, true);
	//p($i);
	foreach ($moneys as $k => $v) {
		$i++;
		$plan['money'] = (string)$v;
		$plan['time'] = (string)$time;
		$plan['type'] = '2';
		$plan['fee'] = 0;
		$plan['sort'] = (string)($key + 1);
		$plan['sum_money'] = (string)($plan['money'] + $plan['fee']);
		$plan['i'] = $i;
		$list[$key][] = $plan;
		$repayment_time = repayment_time($time, $i, $key + 1, $payment, $interval_time, $interval_time_end, 2, $list_time);
		$time = $repayment_time['time'];
		$i = $repayment_time['i'];
		unset($plan);
	}
	$plan['money'] = (string)$money;
	$plan['time'] = (string)$time;
	$plan['type'] = '1';
	$plan['fee'] = '0';
	$plan['sort'] = (string)($key + 1);
	$plan['i'] = $i;
	$list[$key][] = $plan;
	unset($plan);
}
p($list);

function p($data){
	echo "<pre>";print_r($data);exit;
}

function json($data){
	echo json_encode($data);exit;
}




//问题描述，需要修改这个函数 以防止会出现隔夜情况；
//什么是隔夜情况比如 一个完整的计划是刷三还一  那么不能出现26号刷三笔然后27号还一笔.   应该是26号刷三笔必须在26号还
/**
 * 支付时间计算;
 * @Author tw
 * @param  string $time [description]
 * @param  string $i    [description]
 * @return [type]       [description]
 */
function repayment_time($time='',$i='0',$key='0',$payment='',$interval_time='0',$interval_time_end='5000',$type='1',$list_time=array())
{
	$hour = date("H",strtotime($time));
	// if($hour > 18 || $hour < 9 || ($i > $payment['payment_day_num'] && ($list_time[0]!=$list_time[array_search(date("Y-m-d",strtotime($time)), $list_time)] || ( $i > 2 && $list_time[0]==$list_time[array_search(date("Y-m-d",strtotime($time)), $list_time)]))))
	if($hour > 18 || $hour < 9 || ($i > $payment['payment_day_num'] ))
	{
		//if($i/($payment["payment_pattern"]+1))
		$i = 1;
		$time = date("Y-m-d",strtotime("$time"));
		$k = array_search($time, $list_time);
		$time = $list_time[$k+1];
		$time = date("Y-m-d H:i:s",strtotime(date("Y-m-d 09:00:00",strtotime($time))) + rand(0,$interval_time_end));
	}
	elseif($key==0)
	{
		if(date("Y-m-d",strtotime("$time"))==date("Y-m-d") && date('H')>9 && date('H')<18)
		{
			$time = date("Y-m-d H:i:s",time()+ rand(100,600));
		}
		else
		{
			$time = date("Y-m-d H:i:s",strtotime(date("Y-m-d 09:00:00",strtotime("$time"))) + rand(0,3600));
		}
		$paytime = $time;
	}
	else
	{
		$time = date("Y-m-d H:i:s",strtotime($time) + rand($interval_time,$interval_time_end)); //时间
	}

	if($hour>19)
	{
		$time = date("Y-m-d 19:i:s",strtotime($time)); //时间
	}
	return ['time'=>$time,'i'=>$i];

}















/****一下方法都是辅助方法无关紧要不必修改*************************/
/**
 * [计划生成]
 * @Author tw
 * @param  [type] $money     [金额]
 * @param  [type] $num       [笔数]
 * @param  [type] $money_min [最小金额]
 * @param  [type] $money_max [最大金额]
 * @param  string $min       [description]
 * @param  string $max       [description]
 * @param  string $decimals  [是否有小数]
 * @return [type]            [description]
 */
function randnum_new($money,$num,$min_money,$max_money,$min_risk,$max_risk, $decimals= 'false'){
	//halt($max_risk);
	$average = round($money / $num);
	$risk_min_money = $average - $min_money;//风控最小值
	// $max_risk = $max_money - $average;

	$sum = 0;
	$result = array_fill( 1, $num, 0 );
	for( $i = 1; $i < $num; $i++ ){
		$risk = round( (rand( $min_risk, $max_risk )) / 2 );
		if($risk > $risk_min_money)
		{
			$risk = $risk_min_money/2;
		}
		if( $sum > 0 ){
			$max = 0;
			$min = 0 - $risk;
		}elseif( $sum < 0 ){
			$min = 0;
			$max = $risk;
		}else{
			$max = $risk;
			$min = 0 - $risk;
		}
		$random = rand( $min, $max );//+rand(-999,999)

		if($decimals)
		{
			$random = $random.".".rand(0,9);
		}
		$sum += $random;
		$result[$i] = toFixed($average + $random);
		if($result[$i] < $min_money)
		{
			$result[$i] = $min_money;
		}
		elseif($result[$i]>$max_money)
		{
			$result[$i] = $max_money;
		}
	}

	//最后一份的份额由前面的结果决定，以保证各份的总和为指定值
	$result[$num] = toFixed($money - array_sum($result));
	foreach( $result as $temp ){
		$data[] = $temp;
	}
	//halt($data);
	return $data;
}

function toFixed($val){
	if(strpos($val,".")!==false){
		return $val;
	}else{
		return $val.".0";
	}
}
?>